using System;
using UnityEngine;

namespace LS.SimpleInput
{
    public class SimpleInput : MonoBehaviour
    {
        public static SimpleInput Instance { get; private set; }

        public static event Action<Gesture> OnAnyGestrue;
        public static event Action<Gesture> OnAnyGestrueCancle;
        public static event Action<Gesture> OnInput;
        public static event Action<Gesture> OnInputStart;
        public static event Action<Gesture> OnInputEnd;
        public static event Action<Gesture> OnInputCancle;
        public static event Action<Gesture> OnSimpleTap;
        public static event Action<Gesture> OnDoubleTap;
        public static event Action<Gesture> OnLongTapStart;
        public static event Action<Gesture> OnLongTap;
        public static event Action<Gesture> OnLongTapEnd;
        public static event Action<Gesture> OnLongTapCancle;
        public static event Action<Gesture> OnSwipeStart;
        public static event Action<Gesture> OnSwipe;
        public static event Action<Gesture> OnSwipeEnd;
        public static event Action<Gesture> OnSwipeCancle;

        public UserInputSetting setting = new UserInputSetting();

        private UserInputEventSystem m_UserInput;

        private void Awake()
        {
            if (Instance != null && Instance != this)
            {
                Debug.LogWarning("SimpleInput cannot be created repeatedly!");
                this.enabled = false;
                return;
            }

            m_UserInput = new UserInputEventSystem(setting);
            m_UserInput.OnAnyGestrue+= OnAnyGestrueEvent;
            m_UserInput.OnAnyGestrueCancle+= OnAnyGestrueCancleEvent;
            m_UserInput.OnInput+= OnInputEvent;
            m_UserInput.OnInputStart+= OnInputStartEvent;
            m_UserInput.OnInputEnd+= OnInputEndEvent;
            m_UserInput.OnInputCancle+= OnInputCancleEvent;
            m_UserInput.OnSimpleTap+= OnSimpleTapEvent;
            m_UserInput.OnDoubleTap+= OnDoubleTapEvent;
            m_UserInput.OnLongTapStart+= OnLongTapStartEvent;
            m_UserInput.OnLongTap+= OnLongTapEvent;
            m_UserInput.OnLongTapEnd+= OnLongTapEndEvent;
            m_UserInput.OnLongTapCancle+= OnLongTapCancleEvent;
            m_UserInput.OnSwipeStart+= OnSwipeStartEvent;
            m_UserInput.OnSwipe+= OnSwipeEvent;
            m_UserInput.OnSwipeEnd+= OnSwipeEndEvent;
            m_UserInput.OnSwipeCancle += OnSwipeCancleEvent;

        }

        private void OnEnable()
        {
            if (Instance != null && Instance != this)
            {
                Debug.LogWarning("SimpleInput cannot be created repeatedly!");
                this.enabled = false;
                return;
            }
        }

        private void Update()
        {
            m_UserInput.Update();
        }

        private void OnAnyGestrueEvent(Gesture gesture)
        {
            OnAnyGestrue?.Invoke(gesture);
        }

        private void OnAnyGestrueCancleEvent(Gesture gesture)
        {
            OnAnyGestrueCancle?.Invoke(gesture);
        }

        private void OnInputEvent(Gesture gesture)
        {
            OnInput?.Invoke(gesture);
        }

        private void OnInputStartEvent(Gesture gesture)
        {
            OnInputStart?.Invoke(gesture);
        }

        private void OnInputEndEvent(Gesture gesture)
        {
            OnInputEnd?.Invoke(gesture);
        }

        private void OnInputCancleEvent(Gesture gesture)
        {
            OnInputCancle?.Invoke(gesture);
        }

        private void OnSimpleTapEvent(Gesture gesture)
        {
            OnSimpleTap?.Invoke(gesture);
        }

        private void OnDoubleTapEvent(Gesture gesture)
        {
            OnDoubleTap?.Invoke(gesture);
        }

        private void OnLongTapStartEvent(Gesture gesture)
        {
            OnLongTapStart?.Invoke(gesture);
        }

        private void OnLongTapEvent(Gesture gesture)
        {
            OnLongTap?.Invoke(gesture);
        }

        private void OnLongTapEndEvent(Gesture gesture)
        {
            OnLongTapEnd?.Invoke(gesture);
        }

        private void OnLongTapCancleEvent(Gesture gesture)
        {
            OnLongTapCancle?.Invoke(gesture);
        }

        private void OnSwipeStartEvent(Gesture gesture)
        {
            OnSwipeStart?.Invoke(gesture);
        }

        private void OnSwipeEvent(Gesture gesture)
        {
            OnSwipe?.Invoke(gesture);
        }

        private void OnSwipeEndEvent(Gesture gesture)
        {
            OnSwipeEnd?.Invoke(gesture);
        }

        private void OnSwipeCancleEvent(Gesture gesture) 
        {
            OnSwipeCancle?.Invoke(gesture);
        }
    }
}
